package com.zxl.yjb.fragment.my;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.my.BlacklistActivity;
import com.zxl.yjb.activity.my.CouponActivity;
import com.zxl.yjb.activity.my.FeedbackActivity;
import com.zxl.yjb.activity.my.MyDataActivity;
import com.zxl.yjb.activity.my.MyFriendsActivity;
import com.zxl.yjb.activity.my.MyWalletActivity;
import com.zxl.yjb.adapter.MyFragmentCommentAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.LazyBaseFragment;
import com.zxl.yjb.bean.CommentBean;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.HostUrl;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.widget.CircleImageView;
import com.zxl.yjb.widget.flexbox.FlexboxLayout;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class MyFragment extends LazyBaseFragment implements View.OnClickListener, OnRefreshLoadMoreListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private RecyclerView rvCommentList;//评论列表
    private TextView tvPageTitle;
    private ImageView ivConfirm2;
    private TextView tvConfirm;
    private CircleImageView ivMyHeader;
    private TextView tvMyName;
    private LinearLayout llFriends;
    private LinearLayout llWallet;
    private LinearLayout llBlacklist;
    private LinearLayout llCoupon;
    private TextView tvComment;
    private TextView tvScore;
    private FlexboxLayout flexboxEnsure;
    private View headerVeiw;
    private ImageView ivEnter;
    private LinearLayout llMyInfo;
    private TextView tvMySignature;
    private LoginSucessBean loginSucessBean;
    private MyFragmentCommentAdapter adapter;
    private int currentPage = 1;
    private int pageSize = 20;
    private SmartRefreshLayout refreshLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        //title部分
        rvCommentList = view.findViewById(R.id.rv_comment_list);
        refreshLayout = view.findViewById(R.id.refresh_layout);
        tvPageTitle = view.findViewById(R.id.tv_page_title);
        ivConfirm2 = view.findViewById(R.id.iv_confirm2);
        tvConfirm = view.findViewById(R.id.tv_confirm);
        tvCancel.setVisibility(View.VISIBLE);
        Drawable drawable = getActivity().getResources().getDrawable(R.mipmap.feedback);
        drawable.setBounds(0,0,drawable.getMinimumWidth()+10,drawable.getMinimumHeight()+10);
        tvCancel.setCompoundDrawables(drawable,null,null,null);
        tvCancel.setText("意见反馈");
        ivBack.setVisibility(View.GONE);

        //评论列表以上部分
        headerVeiw = headerVeiw.inflate(getContext(), R.layout.myfragment_header, null);
        llMyInfo = headerVeiw.findViewById(R.id.ll_my_info);
        ivMyHeader = headerVeiw.findViewById(R.id.iv_my_header);
        tvMyName = headerVeiw.findViewById(R.id.tv_my_name);
        tvMySignature = headerVeiw.findViewById(R.id.tv_my_signature);
        llFriends = headerVeiw.findViewById(R.id.ll_friends);
        llWallet = headerVeiw.findViewById(R.id.ll_wallet);
        llBlacklist = headerVeiw.findViewById(R.id.ll_blacklist);
        llCoupon = headerVeiw.findViewById(R.id.ll_coupon);
        tvComment = headerVeiw.findViewById(R.id.tv_comment);
        tvScore = headerVeiw.findViewById(R.id.tv_score);
        ivEnter = headerVeiw.findViewById(R.id.iv_enter);


        flexboxEnsure = headerVeiw.findViewById(R.id.flexbox_ensure);
        //初始化title部分的数据；
        tvPageTitle.setText(R.string.my);
        ivConfirm2.setImageResource(R.mipmap.zy_kf);
        ivConfirm2.setVisibility(View.VISIBLE);
        tvConfirm.setText(R.string.kefu);
        tvConfirm.setVisibility(View.VISIBLE);
        tvConfirm.setTextColor(getResources().getColor(R.color.common_text_color));
    }

    @Override
    protected void main() {
        setUserInfo();
        initAdapter();
    }

    public void setUserInfo() {
        Object bean = SaveBeanUtil.getBean(mContext, Const.VIPINFO);
        if (bean != null) {
            loginSucessBean = (LoginSucessBean) bean;
        }
        //设置个人资料；
        if (loginSucessBean == null)
            return;
        tvMyName.setText(loginSucessBean.getNickname());
        tvMySignature.setText("手机：" + loginSucessBean.getPhone());
        GlideApp.with(getContext()).load(HostUrl.HOSTPIC + loginSucessBean.getFace_img()).into(ivMyHeader);
    }

    private void initAdapter() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCommentList.setLayoutManager(layoutManager);
        //创建适配器
        adapter = new MyFragmentCommentAdapter(R.layout.item_comment);
        adapter.addHeaderView(headerVeiw);
        //给RecyclerView设置适配器
        rvCommentList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ToastUtils.show(getContext(),position+"");
            }
        });
        refreshLayout.setOnRefreshLoadMoreListener(this);

    }

    @Override
    protected void lazyFetchData() {
        super.lazyFetchData();
        getCommenList(false);
    }

    /**
     * 获取评论列表
     *
     * @param isRefresh
     */
    private void getCommenList(final boolean isRefresh) {
        DataManager dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("page", currentPage + "");
        map.put("count", pageSize + "");
        map.put("uid", loginSucessBean.getUid() + "");
        ObserverOnNextListener<CommentBean> listener = new ObserverOnNextListener<CommentBean>() {
            @Override
            public void onNext(CommentBean commenBean) {
                tvComment.setText(commenBean.getTotal_count());
                tvScore.setText(commenBean.getEval_avg());
                currentPage++;
                if (isRefresh) {
                    adapter.setNewData(commenBean.getEval_arr());
                    refreshLayout.finishRefresh();
                } else {
                    adapter.addData(commenBean.getEval_arr());
                    refreshLayout.finishLoadMore();
                    if (commenBean.getEval_arr().size() < pageSize) {
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }
            }
        };
        dataManager.evaluate_other2uid_list(new ProgressObserver<CommentBean>(listener, mContext) {
            @Override
            public void showDialog() {
            }
        }, map);

    }

    @Override
    protected void initEvent() {
        ivConfirm2.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        llMyInfo.setOnClickListener(this);
        llFriends.setOnClickListener(this);
        llWallet.setOnClickListener(this);
        llBlacklist.setOnClickListener(this);
        llCoupon.setOnClickListener(this);
        ivEnter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.iv_confirm2://客服
                callKeFe();
                break;
            case R.id.tv_confirm://客服
                callKeFe();
                break;
            case R.id.iv_my_header:
                break;
            case R.id.ll_friends://好友
                intent.setClass(getActivity(), MyFriendsActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_wallet://钱包
                intent.setClass(getActivity(), MyWalletActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_blacklist://黑名单
                intent.setClass(getActivity(), BlacklistActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_coupon://我的优惠券
                intent.setClass(getActivity(), CouponActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_enter:
                break;
            case R.id.ll_my_info://我的信息
                intent.setClass(getActivity(), MyDataActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 】
     * 拨打客服电话
     */
    private void callKeFe() {
        ImageView kfPhone = new ImageView(getContext());
        kfPhone.setImageResource(R.mipmap.zy_dh);
        DisplayUtil.diaplayDialogWithView(getContext(), "客服电话", loginSucessBean.getCustomservice(), "拨打", "取消", kfPhone, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + loginSucessBean.getCustomservice());
                intent.setData(data);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore(5000);
        getCommenList(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh(5000);
        currentPage = 1;
        getCommenList(true);
        refreshLayout.setNoMoreData(false);
    }

    public void refrensh() {
        setUserInfo();
    }


    @OnClick({R.id.iv_back,R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
            case R.id.tv_cancel:
                Intent intent = new Intent(getActivity(),FeedbackActivity.class);
                getActivity().startActivity(intent);
                break;
        }
    }


}
